<?php
// database configuration file
session_start();
ob_start();
// MySQL settings
$db_user    = "root"; // MySQL username
$db_password    = ""; // MySQL password
$db_host    = "localhost"; // MySQL host, mostly localhost
$db_db      = "video"; // MySQL database

function process_text($text) {
	$text = str_replace('\"', '"', $text);
	// $text = str_replace("<", "\<", $text);
	return $text; 
}

function mysql_prepare($text) {
	$text = stripslashes($text);
	$text = mysql_escape_string($text);
	return $text; 
}

// function to insert video to the database server
function insert_video($artist, $song, $code) {
	db_connect();
	$artist = mysql_prepare($artist); 
	$song = mysql_prepare($song);
	$code = mysql_prepare($code);
	// timestamp example: 2007-12-24 18:27:00
	date_default_timezone_set('UTC');
	$date = date('Y-m-d H:m:s');
	
	$sql = "insert into videos VALUES (null, '$artist', '$song',0, '$code', '','',0,'$date')";
	mysql_query($sql) or die ("Query SQL: $sql \n Unable to execute query because ".mysql_error()); 
}

// function to connect to the database server
function db_connect() {	global $db_host, $db_user, $db_password, $db_db; 	
	mysql_connect($db_host, $db_user, $db_password);
	mysql_select_db($db_db) or die ("Unable to select database"); 
}

// function to extract youtube code 
function extract_url($code) {
  $value_pos = strpos($code, "value=\"");
  $code = substr($code, $value_pos+strlen('value="'));
  $end_pos = strpos($code, '"'); 
  
  
  $url = substr($code, 0, $end_pos);
  
  preg_match('/[^\/]+\/\/[^\/]+\/[^\/]+\/([^&]+)/', $url, $matches);
  
  return $matches[1]; 
}

// function to exec sql query
function exec_query($sql) {
	$result = mysql_query($sql) or die ("Query SQL: $sql \n Unable to execute query because ".mysql_error());
	$row = mysql_fetch_assoc($result);
	return $row; 
}

// function to get a sql recurse query
function exec_results($sql) {
	$result = mysql_query($sql) or die ("Result SQL: $sql \n Unable to execute query because ".mysql_error());
	return $result; 
}

// function for validating youtube embedded video
function validateEmbeddedCode($html) {
	$parser = new HtmlParser($html);
	
	// iterate through the parser step by step
	$parser->parse();
	
	// check for <object width="425" height="355">
	if (!($parser->iNodeName == 'object' && $parser->iNodeAttributes['width'] == '425' && $parser->iNodeAttributes['height']='355')) {
		return 0; 
	}
	$parser->parse();
	
	// check for <param name="movie" value="http://www.youtube.com/~/">
	if (!($parser->iNodeName == 'param' && $parser->iNodeAttributes['name'] == 'movie' && preg_match('/www.youtube.com/', $parser->iNodeAttributes['value']))) {
		return 0; 
	}
	$parser->parse();
	
	// check for </param>
	if (!($parser->iNodeName == 'param')) {
		return 0; 
	}
	
	$parser->parse();
	// check for <param name="wmode" value="transparent">
	if (!($parser->iNodeName == 'param' && $parser->iNodeAttributes['value'] == 'transparent'
		  && $parser->iNodeAttributes['name'] == 'wmode')) {
		return 0; 
	}
	
	$parser->parse();
	// check for </param>
	if (!($parser->iNodeName == 'param')) {
		return 0; 
	}
	
	$parser->parse();
	// check for <embed src="http://www.youtube.com/v/qSLvcJ4I1mw&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355">
	if (!($parser->iNodeName = 'embed' && preg_match('/www.youtube.com/', $parser->iNodeAttributes['src']) && 
	$parser->iNodeAttributes['type'] == 'application/x-shockwave-flash' && $parser->iNodeAttributes['wmode'] == 'transparent' 
	&& $parser->iNodeAttributes['width'] == '425' && $parser->iNodeAttributes['height'] == '355')) {
		die ('Invalid <embed src="http://www.youtube.com/v/qSLvcJ4I1mw&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355">');
	}
	
	$parser->parse();
	// check for </param>
	if (!($parser->iNodeName == 'embed')) {
		return 0; 
	}
	
	$parser->parse();
	// check for </object>
	if (!($parser->iNodeName == 'object')) {
		return 0; 
	}
	
	return 1; 
}

// function to return the "XXX (days/hours/minutes/sec) ago
function getTimeDuration($last_timestamp) {
	$current_timestamp = time();
	
	// 1) check to see if it's "days" ago 
	$duration = ($current_timestamp - $last_timestamp) / 86400;
	if ($duration > 1) {
		return round($duration)." days"; 
	}
	elseif ($duration == 1) {
		return round($duration)." day";
	}
	
	// 2) then, check to see if it's "hours" ago
	$duration = ($current_timestamp - $last_timestamp) / 3600;
	if ($duration > 1) {
		return round($duration)." hours";		
	}
	elseif ($duration == 1) {
		return round($duration)." day"; 
	}
	
	// 3) "minutes" ago
	$duration = ($current_timestamp - $last_timestamp) / 60;
	if ($duration > 1) {
		return round($duration)." minutes"; 
	}
	elseif ($duration == 1) {
		return round($duration)." minute"; 
	}
	
	// 4) seconds ago
	return round($duration)." seconds"; 
}
?>
